﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _Specialtapsoftaperthreadsforgascylinders
    {

        public static void Specialtapsoftaperthreadsforgascylinders(Dictionary<string, Object> data)//螺母丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
                                                                           //作图
                                                                           //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double L = Convert.ToDouble(data["oal"]);
            double l = Convert.ToDouble(data["lu"]);
            double L0 = Convert.ToDouble(data["thl"]);
            double L1 = Convert.ToDouble(data["tcl"]);
            double d1 = Convert.ToDouble(data["dconms"]);
            double a = Convert.ToDouble(data["drvsc"]);
            double L2 = Convert.ToDouble(data["drvl"]);
            double d = Convert.ToDouble(data["dc"]);
            double h = Convert.ToDouble(data["h"]);

            /*double L = 75;
            double l = 35;
            double L0 = 30;
            double L1 = 6;
            double d1 = 16;
            double a = 12.5;
            double L2 = 16;
            double d = 19.2;
            double h = 0.96;*/

            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + L + 6, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 3 + L0, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + d / 2);
            Vector2 v5 = new Vector2(v3.X - (L0 - L1), v4.Y - (L0 - L1) * 3 / 50);
            Vector2 v6 = new Vector2(v5.X - L1, v5.Y - L1 * Math.Tan(15 * pi / 180));
            Vector2 v7 = new Vector2(v3.X + (l - L0), v4.Y + (l - L0) * 3 / 50);
            Vector2 v8 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v9 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v10 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v11 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v12 = new Vector2(v7.X + (v7.Y - d1 / 2) / 2.676, v3.Y + d1 / 2);
            Vector2 v13 = new Vector2(v6.X + L - d1 / 7.611, v12.Y);
            Vector2 v14 = new Vector2(v6.X + L, v13.Y - d1 / 7.611);
            Vector2 v15 = new Vector2(v14.X, v14.Y * -1);
            Vector2 v16 = new Vector2(v13.X, v13.Y * -1);
            Vector2 v17 = new Vector2(v12.X, v12.Y * -1);
            Vector2 v18 = new Vector2(v7.X, v17.Y - (v7.Y - d1 / 2) / 2.676);
            Vector2 v19 = new Vector2(v18.X, v18.Y * -1);
            Vector2 v20 = new Vector2(v14.X - L2, v13.Y);
            Vector2 v21 = new Vector2(v20.X, v3.Y + (a / Math.Sin(45 * pi / 180)-d1)/2);
            Vector2 v22 = new Vector2(v13.X, v21.Y);
            Vector2 v23 = new Vector2(v14.X, v14.Y - (v14.Y - v22.Y) / 2);
            Vector2 v24 = new Vector2(v20.X, v20.Y * -1);
            Vector2 v25 = new Vector2(v21.X, v21.Y * -1);
            Vector2 v26 = new Vector2(v22.X, v22.Y * -1);
            Vector2 v27 = new Vector2(v23.X, v23.Y * -1);
            Vector2 v28 = new Vector2(v3.X, v3.Y + d / 2 + 3);
            Vector2 v29 = new Vector2(v28.X, v28.Y * -1);
            Vector2 v30 = new Vector2(v7.X, v7.Y- h);
            Vector2 a30 = new Vector2(v6.X, v30.Y - l * 3 / 50);
            Vector2 v31 = fun.Inters(v6, v5, v30, a30);
            Vector2 v32 = new Vector2(v30.X, v30.Y * -1);
            Vector2 v33 = new Vector2(v31.X, v31.Y * -1);

            Vector2 v34 = new Vector2(v2.X + L / 3, v2.Y);
            Vector2 v35 = new Vector2(v34.X + a / Math.Sin(45 * pi / 180) + 6, v34.Y);
            Vector2 v36 = new Vector2(v34.X + a / Math.Sin(45 * pi / 180) / 2 + 3, v34.Y);
            Vector2 v37 = new Vector2(v36.X, v36.Y +a / Math.Sin(45 * pi / 180) / 2 + 3);
            Vector2 v38 = new Vector2(v37.X, v37.Y * -1);
            Vector2 v39 = new Vector2(v34.X + 3+ (a / Math.Sin(45 * pi / 180) - d1) / 2, v22.Y);
            Vector2 v40 = new Vector2(v39.X, v39.Y * -1);
            Vector2 v41 = new Vector2(v36.X - (v39.Y - v36.Y), v16.Y);
            Vector2 v42 = new Vector2(v36.X + (v39.Y - v36.Y), v16.Y);
            Vector2 v43 = new Vector2(v35.X - 3, v40.Y);
            Vector2 v44 = new Vector2(v43.X, v39.Y);
            Vector2 v45 = new Vector2(v42.X, v42.Y * -1);
            Vector2 v46 = new Vector2(v41.X, v45.Y);


            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            fun.PolyFill(dxf, line2, 0.3, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v19));
            line3.Vertexes.Add(new LwPolylineVertex(v12));
            line3.Vertexes.Add(new LwPolylineVertex(v13));
            line3.Vertexes.Add(new LwPolylineVertex(v14));
            line3.Vertexes.Add(new LwPolylineVertex(v15));
            line3.Vertexes.Add(new LwPolylineVertex(v16));
            line3.Vertexes.Add(new LwPolylineVertex(v17));
            line3.Vertexes.Add(new LwPolylineVertex(v18));
            line3.Vertexes[0].Bulge = Math.Tan(90 * pi / 180 / 4);
            line3.Vertexes[6].Bulge = Math.Tan(90 * pi / 180 / 4);
            fun.PolyFill(dxf, line3, 0.3, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v20));
            line4.Vertexes.Add(new LwPolylineVertex(v21));
            line4.Vertexes.Add(new LwPolylineVertex(v22));
            line4.Vertexes.Add(new LwPolylineVertex(v23));
            line4.Vertexes.Add(new LwPolylineVertex(v22));
            line4.Vertexes.Add(new LwPolylineVertex(v26));
            line4.Vertexes.Add(new LwPolylineVertex(v27));
            line4.Vertexes.Add(new LwPolylineVertex(v26));
            line4.Vertexes.Add(new LwPolylineVertex(v25));
            line4.Vertexes.Add(new LwPolylineVertex(v24));
            fun.PolyFill(dxf, line4, 0.3, contour);

            Line line5 = new Line(v28, v29);
            line5.Layer = centre;
            line5.Linetype = Linetype.DashDot;
            line5.LinetypeScale = 3;
            dxf.AddEntity(line5);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v31));
            line6.Vertexes.Add(new LwPolylineVertex(v30));
            line6.Vertexes.Add(new LwPolylineVertex(v32));
            line6.Vertexes.Add(new LwPolylineVertex(v33));
            fun.PolyFill(dxf, line6, 0.1, contour);

            //
            Line line7 = new Line(v34, v35);
            line7.Layer = centre;
            line7.Linetype = Linetype.DashDot;
            line7.LinetypeScale = 3;
            dxf.AddEntity(line7);

            Line line8 = new Line(v37, v38);
            line8.Layer = centre;
            line8.Linetype = Linetype.DashDot;
            line8.LinetypeScale = 3;
            dxf.AddEntity(line8);

            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v39));
            line9.Vertexes.Add(new LwPolylineVertex(v40));
            line9.Vertexes.Add(new LwPolylineVertex(v41));
            line9.Vertexes.Add(new LwPolylineVertex(v42));
            line9.Vertexes.Add(new LwPolylineVertex(v43));
            line9.Vertexes.Add(new LwPolylineVertex(v44));
            line9.Vertexes.Add(new LwPolylineVertex(v45));
            line9.Vertexes.Add(new LwPolylineVertex(v46));
            line9.Vertexes.Add(new LwPolylineVertex(v39));
            fun.PolyFill(dxf, line9, 0.3, contour);




            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v39));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v40));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v41));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v42));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v43));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v44));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v45));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v46));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v39));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int I = 0; I < hnumst1; I++)
            {
                htype1[I] = hatchpoly1.Explode()[I].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[I] == "Line")
                {
                    hline1[I] = (Line)hatchpoly1.Explode()[I];
                }
                if (htype1[I] == "Arc")
                {
                    harc1[I] = (Arc)hatchpoly1.Explode()[I];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);



            //标注

            double height = 3;
            double arrowsize = 1;
            double exline = 2;




            fun.dimrot1(dxf, v4, v9, 90, 0, 1, height, arrowsize, exline, "DC=" + d, "", "");

            fun.dimrot1(dxf, v20, v24, 90, d/4, 1, height, arrowsize, exline, "DCONMS=" + d1, "", "");

            fun.dimrot1(dxf, v11, v10, 180, d/2, 1, height, arrowsize, exline, "TCL=" + L1, "", "");

            fun.dimrot1(dxf, v11, v9, 180, d, 1, height, arrowsize, exline, "THL=" + L0, "", "");

            fun.dimrot1(dxf, v11, v8, 180, d*1.5, 1, height, arrowsize, exline, "LU=" + l, "", "");

            fun.dimrot1(dxf, v11, v15, 180, d*2, 1, height, arrowsize, exline, "OAL=" + L, "", "");

            fun.dimrot1(dxf, v24, v15, 180, d /4, 1, height, arrowsize, exline, "DRVSC=" + L2, "", "");

            fun.jdbz1(dxf, new Line(v5, fun.Polar(v5, -165 * pi / 180, 0.1)), new Line(v5, fun.Polar(v5, 180 * pi / 180, 0.1)),L1*4 , "15°", 3);


            fun.dimrot1(dxf, v41, v44, -135, d / 2, 1, height, arrowsize, exline, "DRVL=" + a, "", "");




            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
